﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Inventory))]

public class Character : MonoBehaviour 
{

	//---------------------------------------------------------------------------------------------
	// Properties
	public string playerName;
	
	public int location_x = 50;
	public int location_z = 50;

	public bool inSettlement = false;
	

	
	public int gold {get; set;}
	public int carry {get; set;}
	public int maxCarry;
	public int thirst {get; set;}
	public int maxThirst;
	public int hunger {get; set;}
	public int maxHunger;
	public int health {get; set;}
	public int maxHealth;
	public int energy {get; set;}
	public int maxEnergy;
	
	
	bool isMoving = false;
	
	int movement_z;
	int movement_x;
	
	float movespeed = 1f;
	int movedistance = 1;
	float moveDuration = 0.10f;
	Vector3 currentPosition;
	Vector3 desiredPosition;
	float movePercentage;
	float movementStartTime;
	

	//---------------------------------------------------------------------------------------------
	// Members
	public Animator m_animator;
	TurnService m_turnMain;
	terrainGeneration m_terrainMain;
	public GameObject m_navUI;
	public GameObject m_currentTerrain;
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	public delegate void PlayerManager();
	public static event PlayerManager playerMoved;
	public static event PlayerManager playerInSettlement;
	
	//---------------------------------------------------------------------------------------------
	// Init
	
	void Start () 
	{
		PlayerInit();
		m_animator = GetComponent<Animator>();
		//m_navUI = GameObject.Find("Panel - Navigation");
		m_turnMain = GameObject.Find("TurnService").GetComponent<TurnService>();
		m_terrainMain = GameObject.Find("TerrainService").GetComponent<terrainGeneration>();
		TurnService.DayEnd += HungerThirstUpdate;
	}
	//---------------------------------------------------------------------------------------------
	// Getters and Setters
	
	
	//---------------------------------------------------------------------------------------------
	// Core Functions

	void Update () 
	{

		if(m_navUI.activeSelf)
		{
			if (isMoving == false)
			{
				KeyboardControls();
			}
			
		}
	}
	
	//---------------------------------------------------------------------------------------------
	void FixedUpdate()
	{
	
		float moveTimeSinceBegan = Time.time - movementStartTime;
		movePercentage = moveTimeSinceBegan / moveDuration;
		if (isMoving == true)
		{
			transform.position = Vector3.Lerp (currentPosition, desiredPosition, movePercentage);
		}
		if (movePercentage >= 1.0f)
		{
			isMoving = false;
		}
	}
	
	//---------------------------------------------------------------------------------------------
	void StartMovement()
	{
		isMoving = true;
		movementStartTime = Time.time;
		currentPosition = transform.position;
		
	
	}
	
	

	//---------------------------------------------------------------------------------------------
	public void PlayerInit()
	{
		gold = 100;
		hunger = 5;
		thirst = 3;
		energy = 5;
		health = 10;
		
	}

	//---------------------------------------------------------------------------------------------
	public void PlayerControls(int x)
	{
		
		/*
		_timeStartedLerping = Time.time;
		float timeSinceStarted = Time.time - _timeStartedLerping;
		float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
		*/
		
		StartMovement();
		
		
		
		
		if (x == 0)
		{
			if ( checkMovement(location_x, location_z+1) == true )
			{
				CheckTerrain(location_x, location_z+1);
				desiredPosition = (currentPosition + (new Vector3(0,0,1)));
				
				location_z ++;
				if (playerMoved != null)
				{
					playerMoved();
				}
				
			}
			
		}
		if (x == 1)
		{
			if ( checkMovement(location_x, location_z-1) == true )
			{
				CheckTerrain(location_x, location_z-1);
				desiredPosition = (currentPosition + (new Vector3(0,0,-1)));
				location_z --;
				if (playerMoved != null)
				{
					playerMoved();
				}
			}
		}
		if (x == 2)
		{
			if ( checkMovement(location_x -1, location_z) == true )
			{
				CheckTerrain(location_x -1, location_z);
				desiredPosition = (currentPosition + (new Vector3(-1,0,0)));
				location_x --;
				if (playerMoved != null)
				{
					playerMoved();
				}
			}
		}
		if (x == 3)
		{
			if ( checkMovement(location_x+1, location_z) == true )
			{
				CheckTerrain(location_x+1, location_z);
				desiredPosition = (currentPosition + (new Vector3(1,0,0)));
				location_x ++;
				if (playerMoved != null)
				{
					playerMoved();
				}
			}
		}
		
		
		
	
	}
	
	//---------------------------------------------------------------------------------------------
	public bool checkMovement(int x, int z)
	{
		if (m_terrainMain.m_activeTerrain[x,z] == false)
		{
			return false;
		}
		else
		{
			return true;
		}
		
	
	}
	
	//---------------------------------------------------------------------------------------------
	public void KeyboardControls()
	{
		if (Input.GetKeyUp(KeyCode.W))
		{
		 PlayerControls(0);
		}
		if (Input.GetKeyUp(KeyCode.S))
		{
			PlayerControls(1);
		}
		if (Input.GetKeyUp(KeyCode.A))
		{
			PlayerControls(2);
		}
		if (Input.GetKeyUp(KeyCode.D))
		{
			PlayerControls(3);
		}
	}
	
	//---------------------------------------------------------------------------------------------
	void CheckTerrain(int x, int z)
	{
		if (m_terrainMain.m_terrainPieces[x,z].GetComponent<terrainPiece>().settled == true)
		{
			inSettlement = true;
			if (playerInSettlement != null)
			{
				playerInSettlement();
			}
			m_currentTerrain = m_terrainMain.m_terrainPieces[x,z];
		}
		else 
		{
			m_currentTerrain = m_terrainMain.m_terrainPieces[x,z];
			inSettlement = false;
		}
		
	
	}
	
	//---------------------------------------------------------------------------------------------
	
	void HungerThirstUpdate()
	{
		hunger --;
		thirst --;
		energy --;
		if (hunger <= 0)
		{
			hunger = 0;
			health --;
		}
		if (thirst <= 0)
		{
			thirst = 0;
			health --;
		}
		if (energy <=0)
		{
			energy = 0;
			health --;
		}
		
		if (health <= 0)
		{
			Debug.Log (name + " Game Over " + this);
		}
	
	}
	//---------------------------------------------------------------------------------------------
}
